package com.example;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Reflect {

    public static void main(String[] args) throws Exception {
        //获取Person类的Class对象
        Class<?> classType = Class.forName("com.example.Person");

        //调用Person类的两个参数构造方法生成对象
        Constructor constructor = classType.getConstructor(new Class[]{String.class, int.class});
        Object object = constructor.newInstance(new Object[]{"Dean",25});

        //获取setId方法
        Method setId = classType.getMethod("setId", new Class[]{long.class});

        //调用setId方法设置Id
        setId.invoke(object, new Object[]{10});

        //调用toString输出结果
        Method toString = classType.getMethod("toString", new Class[]{});
        String result = (String) toString.invoke(object, new Object[]{});

        System.out.println(result);

//        classType.getDeclaredMethod("say");

        StringBuffer b=new StringBuffer();
        b.substring(0);


    }
}
